<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Created by JetBrains PhpStorm.
 * User: b4by
 * Date: 1/23/13
 * Time: 10:17 AM
 * To change this template use File | Settings | File Templates.
 */
class ViewCatalogue extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('model_product');
		$this->load->model('model_page');
	}

	public function index($cat_id, $page=1) {
//		echo $this->uri->ruri_string();
		$this->data['catalogue'] = $this->model_catalogue->get_info($cat_id);
		if(!$this->data['catalogue']) show_error (lang('catalogue_not_found'));
		if($this->data['catalogue']->type==0){
			$pagination = pagination_init($this->model_page->get_total(array(
				'cat_id'	=> $cat_id
			)), $page);
			$data= $this->model_page->get_list(array(
				'where'	=> array('cat_id'	=>$cat_id),
				'limit'=> array($pagination->from,$pagination->limit)
			));
			foreach($data as $page) $page->thumb = image_url($page->thumb);
		}else{
			$pagination = pagination_init($this->model_product->get_total(array(
				'c.cat_id'	=> $cat_id
				)), $page);
			$data = $this->model_product->get_list($this->user_lang->id, array(
				'order' => array('p.id', 'desc'),
				'limit'	=> array($pagination->from,$pagination->limit)
				), $this->location, $cat_id);

			foreach ($data as &$product) {
				$product->thumb = image_url($product->thumb);
			}
		}
		$this->data['site_title'] = $this->data['catalogue']->info[$this->user_lang->id]->name . ' | ' . $this->siteConfig->site_title;
		$this->data['pagination'] = $pagination;
		$this->data['data'] = $data;
		$this->data['temp'] = 'catalogue/index';
		$this->load_view();
	}

//	public function _best_buys($cat_id) {
//		$this->load->model('model_product');
//		$this->load->helper('date');
//		
//		return $this->model_product->get_list($this->user_lang->id, array(
//			'where' => array('end >=' => now()),
//			'order' => array('total_orders', 'desc'),
//			'limit' => array(0, $this->siteConfig->best_buys_num)
//		), $this->location, $cat_id);
//	}

}